<!--
 * @FilePath     : /study_code/layui/12-7-2.html
 * @Description  : 主动提交一个表单
 * @Date         : 2025-04-07 16:07:21
 * @Author       : Fang YingGui
 * @LastEditors  : fangyinggui 18715384437@163.com
 * @LastEditTime : 2025-04-07 17:06:13
-->

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>documentName</title>
    <link rel="stylesheet" href="/layui/dist/css/layui.css" />
  </head>
  <body>
    <fieldset class="layui-elem-field">
      <legend>表单内部</legend>
      <div class="layui-field-box">
        <form class="layui-form" lay-filter="form-demo-submit">
          <input type="text" name="nickname" lay-verify="required" class="layui-input" />
        </form>
      </div>
    </fieldset>
    <button class="layui-btn" id="test-btn-submit">任意位置按钮</button>

    <script src="/layui/dist/layui.js"></script>
    <script>
      layui.use(function () {
        var $ = layui.$
        var form = layui.form
        var layer = layui.layer
        // 任意位置按钮触发提交
        $('#test-btn-submit').on('click', function () {
          form.submit('form-demo-submit', function (data) {
            var field = data.field // 获取表单全部字段值
            console.log(data) // 回调函数返回的 data 参数和提交事件中返回的一致
            // 执行提交
            layer.confirm('确定提交吗？', function (index) {
              layer.close(index) // 关闭确认提示框
              // 显示填写结果，仅作演示用
              layer.alert(JSON.stringify(field), {
                title: '当前填写的字段值',
              })
              // 此处可执行 Ajax 等操作
              // …
            })
          })
          return false
        })
      })
    </script>
  </body>
</html>
